package com.rocket.bedroom.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rocket.bedroom.exceptions.RepeatNameException;
import com.rocket.bedroom.pojo.BerthEntity;
import com.rocket.bedroom.pojo.BlockEntity;
import com.rocket.bedroom.service.IBlockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 21:02 2020/5/3
 * @ModifyBy:
 */
@RestController
@RequestMapping(value = "/block")
public class BlockController {

    Logger logger = LoggerFactory.getLogger(BlockController.class);

    @Resource
    private IBlockService blockService;


    @GetMapping(value = "/pageBlock")
    public PageInfo<BlockEntity> pageBlock(BlockEntity blockEntity, Integer pageNumber, Integer pageSize) {
        if (null == pageNumber || 0 == pageNumber) {
            pageNumber = 1;
        }
        if (null == pageSize || 0 == pageSize) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);

        LambdaQueryWrapper<BlockEntity> queryWrapper = Wrappers.lambdaQuery();
        if (null != blockEntity) {
            if (!StringUtils.isEmpty(blockEntity.getBlockName())) {
                queryWrapper
                        .likeRight(BlockEntity::getBlockName, blockEntity.getBlockName());
            }
            queryWrapper
                    .orderByAsc(BlockEntity::getBlockId);
        }
        List<BlockEntity> list = blockService.list(queryWrapper);
        return new PageInfo<>(list);
    }

    @PostMapping(value = "/addBlock")
    public int addBlock(BlockEntity blockEntity) {
        logger.debug(blockEntity.toString());
        Assert.notNull(blockEntity, "ADD 楼栋对象 IS null");
        Assert.notNull(blockEntity.getBlockName(), "ADD 楼栋 BLOCK_NAME IS NULL");
        if (0 != blockService.hasBlockName(blockEntity.getBlockName())) {
            throw new RepeatNameException("楼栋名称重复", 500);
        }
        boolean save = blockService.save(blockEntity);
        System.out.println(blockEntity.getBlockId());
        return save ? 1 : -1;
    }

    @GetMapping(value = "/findBlockById/{blockId}")
    public BlockEntity findBlockById(@PathVariable(value = "blockId") Long blockId) {
        Assert.notNull(blockId, "find 楼栋Id IS NULL");
        return blockService.getById(blockId);
    }

    @GetMapping(value = "/hasBlockName/{blockName}")
    public int hasBlockName(@PathVariable(value = "blockName") String blockName) {
        return blockService.hasBlockName(blockName);
    }


    @PutMapping(value = "/modifyBlock")
    public int modifyBlock(BlockEntity blockEntity) {
        Assert.notNull(blockEntity, "UPDATE 楼栋对象 IS null");
        Assert.notNull(blockEntity.getBlockId(), "UPDATE 楼栋 ID IS NULL");
        if (0 != blockService.hasBlockName(blockEntity.getBlockName())) {
            throw new RepeatNameException("楼栋名称重复", 500);
        }
        boolean modifyRS = blockService.updateById(blockEntity);
        return modifyRS ? 1 : -1;
    }

}
